<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 目录辅助函数 覆盖
 */

/**
 * 创建文件夹并生成默认index.html
 *
 * @access    public
 * @param     string $path 文件夹路径（可以是多重路径）
 * @return    bool
 */
if (!function_exists('directory_create')) {
    function directory_create($path) {
        if (!@is_dir($path)) {
            directory_create(dirname($path));
            @mkdir($path);
            @chmod($path, 0777);
            //data mixed
            $data = '<html>' . "\n";
            $data .= '<head>' . "\n";
            $data .= '<title>403 Forbidden</title>' . "\n";
            $data .= '</head>' . "\n";
            $data .= '<body>' . "\n\n";
            $data .= '<p>Directory access is forbidden.</p>' . "\n\n";
            $data .= '</body>' . "\n";
            $data .= '</html>';
            //filename string
            $filename = '/index.html';
            $strlast = substr($path, -1);
            if ($strlast == '/' or $strlast == '\\') $filename = 'index.html';
            file_put_contents($path . $filename, $data);
            @chmod($path . '/index.html', 0777);
        } elseif (!is_writeable($path)) {
            return false;
        }
        return true;
    }
}

/************ 以下为CI原有的目录辅助函数 ************/

/**
 * Create a Directory Map
 *
 * Reads the specified directory and builds an array
 * representation of it.  Sub-folders contained with the
 * directory will be mapped as well.
 *
 * @access    public
 * @param     string    path to source
 * @param     int       depth of directories to traverse (0 = fully recursive, 1 = current dir, etc)
 * @return    array
 */
if (!function_exists('directory_map')) {
    function directory_map($source_dir, $directory_depth = 0, $hidden = false) {
        if ($fp = @opendir($source_dir)) {
            $filedata = array();
            $new_depth = $directory_depth - 1;
            $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

            while (false !== ($file = readdir($fp))) {
                // Remove '.', '..', and hidden files [optional]
                if (!trim($file, '.') or ($hidden == false && $file[0] == '.')) {
                    continue;
                }

                if (($directory_depth < 1 or $new_depth > 0) && @is_dir($source_dir . $file)) {
                    $filedata[$file] = directory_map($source_dir . $file . DIRECTORY_SEPARATOR, $new_depth, $hidden);
                } else {
                    $filedata[] = $file;
                }
            }

            closedir($fp);
            return $filedata;
        }

        return false;
    }
}

/* End of file directory_helper.php */
/* Location: ./application/helpers/directory_helper.php */
